package com.bblocks.Interceptor;



import com.bblocks.common.Constant;
import com.bblocks.pojo.ComUser;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 只是把用户信息放入 request，顺带会把citycode 保存
 *
 * @author Jin
 */
@Slf4j
@Component
public class AuthorizationInterceptor implements HandlerInterceptor {

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception
	{
		Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
		ComUser user = (ComUser) authentication.getPrincipal();
		if(user != null) {
			request.setAttribute(Constant.USER,user);
			request.setAttribute(Constant.ACCESS_TOKEN_KEY,request.getHeader(Constant.ACCESS_TOKEN_KEY));
		}
		return true;
	}
}
